package com.link.base.base.order.controller;

import com.link.base.base.order.model.OrderPolicyResult;
import com.link.base.base.order.service.OrderPolicyResultService;
import com.link.base.base.policy.model.PcyResult;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.base.core.basic.util.YmlConstants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 订单政策结果请求处理
 */
@Controller
@RequestMapping("/link/orderPolicyResult")
public class OrderPolicyResultController extends BasicController<OrderPolicyResult> {

    @Resource
    private OrderPolicyResultService orderPolicyResultService;

    @Override
    public BasicService<OrderPolicyResult> getBasicService() throws Exception {
        return orderPolicyResultService;
    }

    /**
     * <p>根据订单id查询促销规则结果信息</p>
     *
     * @author 姚润发
     * @param orderId
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryPcyResultByOrderId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPcyResultByOrderId(Long orderId, HttpSession session,
                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<PcyResult> list = new ArrayList<PcyResult>();
            //Demo走siebel数据，暂时不查促销
            String flag = "Y";
            if (!flag.equals(YmlConstants.getDmsForSiebel())) {
                list = orderPolicyResultService.queryPcyResultByOrderId(orderId);
            }
            result.put("success", true);
            result.put("rows", list);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>根据订单id查询符合条件的促销规则信息</p>
     * @author 姚润发
     * @date 2017/14
     * @param orderId 订单id
     * @param example 查询条件对象，使用政策结果对象
     * @return
     */
    @RequestMapping(value = "/queryPcyRuleByOrderId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryPcyRuleByOrderId(Long orderId, @JsonParam OrderPolicyResult example) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (orderId == null) {
                orderId = Long.parseLong("noMatchId");
            }
            example.setOrderId(orderId);
            List<OrderPolicyResult> list = orderPolicyResultService.queryByExamplePage(example);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(example, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
